/*
 * @author : TF-BJ-C064
 * @creation : 2014-6-27 下午4:30:37
 * @description : 
 *
 */
package com.action.park;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;
import org.apache.log4j.Logger;
import com.base.UserBean;
import com.bean.ParkManager;
import com.bean.ParkOrder;
import com.bean.ParkOrderBilling;
import com.bean.ParkOrderExt;
import com.bean.Userinfo;
import com.dao.ParkOrderDao;
import com.service.ParkManagerService;
import com.service.ParkOrderService;
import com.util.GUtil;

public class MyParkOrderAction extends BaseParkAction {

	private static final Logger log = Logger.getLogger(ParkOrderDao.class);

	ParkOrderService parkOrderService;
	ParkManagerService parkManagerService;

	ParkOrder parkOrder;
	Userinfo info;
	Timestamp ts = new Timestamp(System.currentTimeMillis());
	SimpleDateFormat sdf = new SimpleDateFormat(" yyyy-MM-dd HH:mm:ss ");
	String date = sdf.format(ts);

	public Userinfo getInfo() {
		return info;
	}

	public void setInfo(Userinfo info) {
		this.info = info;
	}

	List<ParkOrder> list;

	Boolean queryMyOrder = true;// true: 收费员查询自己的订单 ; false查询所有订单

	@Override
	public String handle() throws Exception {
		if (id == null)
			return this.query();
		else
			return this.view();
	}

	/**
	 * 订单管理
	 * <p>
	 * Title: query
	 * </p>
	 * <p>
	 * Description: 首先根据session中user.role 判断是停车场管理员还是后台管理员 if(user.role==3)//
	 * 停车场管理员 else if(user.role==1) //停车场收费员 else
	 * if(user.role>UserBean.MAX_PARK_ROLE_ID)//后台管理员
	 * </p>
	 * 
	 * @return
	 * @throws Exception
	 * @see com.action.park.BaseParkAction#query()
	 */
	@Override
	public String query() throws Exception {
		this.setHeaderTitle("订单管理");
		session.put("pname",parkOrderService.getPname(user));
		session.put("status",parkOrderService.getstatus());
		if (user.getRole() == 3) {
			// 停车场管理员
			// 查看本停车场所有的订单
			list = parkOrderService.queryParkOrder(parkOrder, user,page);
			if (list == null) {
				log.info("停车场管理员" + user.getUserCode() + " - "
						+ user.getUserName() + "  查看本停车场所有的订单 list==null");
			} else {
				log.info("停车场管理员" + user.getUserCode() + " - "
						+ user.getUserName() + "  查看本停车场所有的订单 list.size= "
						+ list.size());
			}
		} else if (user.getRole() == 1) {
			// 停车场收费员
			if (queryMyOrder) {// 只查看我的订单
				list = parkOrderService.queryMyOrder(parkOrder, user,page);
			}
		} else if (user.getRole() > UserBean.MAX_PARK_ROLE_ID) {
			// 后台管理员
			list = parkOrderService.query(parkOrder,user,page);// 查看所有订单
		}

		return GUtil.SUCCESS;
	}

	/*
	 * 删除状态为：-3：超时未支付的订单；-2：超时未确认定的订单；-1：管理员确认无法预订；0：订单已取消 (non-Javadoc)
	 * 
	 * @see com.action.park.BaseParkAction#delete()
	 */
	@Override
	public String delete() throws Exception {
		if (id == null || id < 1)
			return GUtil.ERROR;
		else {
			if (parkOrderService.delete(Long.valueOf(id), -4).equals(
					GUtil.SUCCESS))
				this.setMsg("删除成功");
			else
				this.setMsg("删除失败");
		}
		return this.query();

	}

	@Override
	public String view() throws Exception {
		if(id==null || id<1)
			return GUtil.ERROR;
		parkOrder =  (ParkOrder) parkOrderService.get(Long.valueOf(id));
		session.put("park", parkOrder);
		return GUtil.VIEW;
	}

	@Override
	public String add() throws Exception {

		return null;
	}

	@Override
	public String update() throws Exception {
		this.setHeaderTitle("修改");
		if (id == null)
			return GUtil.ERROR;
		if (parkOrder == null) {
			// beforeUpdate
			parkOrder = (ParkOrder) parkOrderService.get(Long.valueOf(id));
			if (parkOrder == null) {
				this.setLogmsg("信息不存在 id=" + id);
				return GUtil.ERROR;
			}
			session.put("parkOrder", parkOrder);
			session.put("id", parkOrder.getPoId());
			return GUtil.EDIT;
		}

		// update
		ParkOrder pk = (ParkOrder) session.get("parkOrder");

		pk.setCarportCount(parkOrder.getCarportCount());
		Timestamp ts = new Timestamp(System.currentTimeMillis());
		pk.setUpdateTime(ts);
		// to update

		if (parkOrderService.update(pk).endsWith(GUtil.SUCCESS)) {
			this.setMsg("修改成功");
			parkOrder = null;
			return this.query();
		} else {
			this.setMsg("修改失败");
			return GUtil.EDIT;
		}
	}

	/*
	 * 列为逃废
	 */
	public String Evasion() throws Exception {
		if (id == null) {
			return GUtil.ERROR;
		}
		if (parkOrder == null) {
			// beforeUpdate
			parkOrder = (ParkOrder) parkOrderService.get(Long.valueOf(id));
			if (parkOrder == null) {
				this.setLogmsg("信息不存在 id=" + id);
				return GUtil.ERROR;
			}
			session.put("parkOrder", parkOrder);
			session.put("id", parkOrder.getPoId());
		}
		// update

		user = (UserBean) session.get("user");
		ParkManager pm = parkManagerService.get(user.getId());// 收费员
		ParkOrder or = (ParkOrder) session.get("parkOrder");
		or.getPoId();// 订单id
		Userinfo userinfo = parkOrder.getUserinfo();// 司机
		or.setParkManagerByLeaveOperatorId(pm);
		Short s = 2;
		or.setStatus(s);
		or.setUpdateTime(ts);

		//
		ParkOrderExt poe = ParkOrderExt.newInstance(or.getExtendInfo());
		if (poe == null) {
			this.setMsg("系统内部错误");
			log.error("ParkOrderExt.newInstance return null " + this.getMsg());
			return GUtil.ERROR;
		}
		ParkOrderBilling pob = new ParkOrderBilling();
		or.setReceivableAmount(pob.CalcOrderBilling(poe, or.getOrderTime(),
				or.getLeaveTime()));
		// 计算价格
		parkOrderService.updateuserandrecord(userinfo, or);
		if (parkOrderService.update(or).endsWith(GUtil.SUCCESS)) {
			this.setMsg("[" + date + "][停车场收费员:" + user.getUserCode() + ","
					+ user.getUserName() + "][把用户:"
					+ or.getUserinfo().getRealName() + "成功列为逃废用户," + "订单号:"
					+ or.getPoNo() + "]");
			or = null;
			userinfo = null;
			return this.query();
		} else {
			this.setMsg("[" + date + "][停车场收费员:" + user.getUserCode() + ","
					+ user.getUserName() + "][把用户:"
					+ or.getUserinfo().getRealName() + "列为逃废用户操作失败" + "订单号:"
					+ or.getPoNo() + "]");
			return this.query();
		}
	}

	/**
	 * @return the parkOrderService
	 */
	public ParkOrderService getParkOrderService() {

		return parkOrderService;
	}

	/**
	 * @param parkOrderService
	 *            the parkOrderService to set
	 */
	public void setParkOrderService(ParkOrderService parkOrderService) {
		this.parkOrderService = parkOrderService;
	}

	/**
	 * @return the parkOrder
	 */
	public ParkOrder getParkOrder() {
		return parkOrder;
	}

	/**
	 * @param parkOrder
	 *            the parkOrder to set
	 */
	public void setParkOrder(ParkOrder parkOrder) {
		this.parkOrder = parkOrder;
	}

	/**
	 * @return the list
	 */
	public List<ParkOrder> getList() {

		return list;
	}

	/**
	 * @param list
	 *            the list to set
	 */
	public void setList(List<ParkOrder> list) {
		this.list = list;
	}

	/**
	 * @return the queryMyOrder
	 */
	public Boolean getQueryMyOrder() {
		return queryMyOrder;
	}

	/**
	 * @param queryMyOrder
	 *            the queryMyOrder to set
	 */
	public void setQueryMyOrder(Boolean queryMyOrder) {
		this.queryMyOrder = queryMyOrder;
	}

	public ParkManagerService getParkManagerService() {
		return parkManagerService;
	}

	public void setParkManagerService(ParkManagerService parkManagerService) {
		this.parkManagerService = parkManagerService;
	}

	@Override
	public boolean validate(Object obj) throws Exception {
		return false;
	}
}
